/* This is the code for generating the PWM signals on 3 of the ports *
 * more specifically PD6, PB1, and PB2 (pins 12, 15, and 16)         *
 *  Frequency = 40,000 kHz                                           *
 *  TOP = (Clock_Speed / (Prescaler * Output_PWM_Frequency)) - 1     *
 *  TOP = 10,000,000 / ( 1 * 40,000 ) - 1 = 249                      *
 *                                                                   *
 *  note: TOP value is only used for the 16bit timers TOP for        *
 *        the 8bit timer is 255 and cannot be changed resulting      *
 *        in a frequency of 39.06 kHz for timer0-A                   *
 */
 
 #include <avr/io.h>
 
 int main(void)
 {
   TCCR0A = 0; // turns off timer 0-A
   TCCR1A = 0; // turns off timer 1-A
   TCCR1B = 0; // turns off timer 1-B

   ICR1 = 249; /* TOP seen above for timer 1 */
   
   TCCR0A = (1 << WGM01) | (1 << WGM00); // sets timer0 to Fast PWM
   TCCR1A = (1 << WGM11); // this and next line sets timer1 to
   TCCR1B = (1 << WGM13) | (1 << WGM12); // Fast PWM with prescaler=1
   
   
   DDRD |= _BV(6);   // enable PD6 as output pin
   DDRB |= _BV(1) | _BV(2); // enables PB1&PB2 as output pins
   
   TCCR0A |= 2 << 6; // sets COM0A(0&1) to 0x10 = non-inverting
   TCCR1A |= 2 << 6; // sets COM1A(0&1) to 0x10 = non-inverting
   TCCR1A |= 2 << 4; // sets COM1B(0&1) to 0x10 = non-inverting
   
   OCR0A = 256 * 1 /2;  // enables 50% duty cycle
   OCR1A = ICR1 * 1 /2; // enables 50% duty cycle
   OCR1B = ICR1 * 1 /2; // enables 50% duty cycle
   
   while(1)
   {
   
   }
   
   return 0;
 }
 
